#include "TabVideoWidget.h"
#include "ui_TabVideoWidget.h"

#include "utils/win32apis.h"
#include "utils/const.h"
#include "utils/ConfMgr.h"
#include "ListEditialog.h"
#include "VideoPlayWidget.h"

#include <QFile>
#include <QMessageBox>

TabVideoWidget::TabVideoWidget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::TabVideoWidget)
    , m_outerVideo(new VideoPlayWidget(this))
{
    ui->setupUi(this);
    ui->videoWidget->play();
    ui->videoWidget->setMuted(true);
    ui->videoWidget->setPlayMode(VideoPlayWidget::PlayMode::CurrentItemInLoop);

    if (utils::ConfMgr::readVideoConfig(m_playList)) {
        ui->videoWidget->setPlayList(m_playList);
        ui->videoWidget->play();
        if (!m_playList.isEmpty()) {
            ui->editCurrent->setText(m_playList[0]);
        }
    }
}

TabVideoWidget::~TabVideoWidget()
{
    m_outerVideo->stop();
    delete m_outerVideo;
    delete ui;
}

void TabVideoWidget::playPrev()
{
    m_outerVideo->playPrev();
}

void TabVideoWidget::playNext()
{
    m_outerVideo->playNext();
}


void TabVideoWidget::on_btnEditPlayList_clicked()
{
    ListEditialog dialog(utils::Text::TitleEditVideo, m_playList, this);
    dialog.setFilter(utils::FileExtFilter::Video);
    connect(&dialog, &ListEditialog::editDone, this, [=](QStringList l){
        m_playList.clear();
        for (QString s : l) {
            m_playList << s;
        }
        utils::ConfMgr::writeVideoConfig(m_playList);
        ui->videoWidget->setPlayList(m_playList);
        ui->videoWidget->play();
        ui->editCurrent->setText(l.isEmpty() ? "" : l[0]);
    });
    dialog.exec();
}

void TabVideoWidget::on_radioMute_toggled(bool checked)
{
    m_outerVideo->setMuted(checked);
}

void TabVideoWidget::on_radioRand_toggled(bool checked)
{
    if (checked) {
        m_outerVideo->setPlayMode(VideoPlayWidget::PlayMode::Random);
    }
}

void TabVideoWidget::on_radioLoop_toggled(bool checked)
{
    if (checked) {
        m_outerVideo->setPlayMode(VideoPlayWidget::PlayMode::Loop);
    }
}

void TabVideoWidget::on_radioSing_toggled(bool checked)
{
    if (checked) {
        m_outerVideo->setPlayMode(VideoPlayWidget::PlayMode::CurrentItemInLoop);
    }
}

void TabVideoWidget::on_btnPlay_clicked()
{
    if (m_playList.isEmpty()) {
        QMessageBox::warning(this, utils::Text::TitleEmptyVideo, utils::Text::TextVideoListEmpty);
        return;
    }
    if (m_outerVideo->isPlaying()) {
        m_outerVideo->stop();
    }
    m_outerVideo->setPlayList(m_playList);
    m_outerVideo->setParent(nullptr);
    m_outerVideo->showFullScreen();
    m_outerVideo->play(ui->videoWidget->currentId());
    utils::Wallpaper::setWindowWallpaper(m_outerVideo->winId());
}

void TabVideoWidget::on_btnStop_clicked()
{
    if (m_outerVideo->isPlaying()) {
        m_outerVideo->stop();
        m_outerVideo->hide();
    }
}

void TabVideoWidget::on_btnNext_clicked()
{
    ui->videoWidget->playNext();
    if (ui->videoWidget->isPlaying()) {
        ui->editCurrent->setText(m_playList[ui->videoWidget->currentId()]);
    }
}

void TabVideoWidget::on_btnPrev_clicked()
{
    ui->videoWidget->playPrev();
    if (ui->videoWidget->isPlaying()) {
        ui->editCurrent->setText(m_playList[ui->videoWidget->currentId()]);
    }
}
